/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Control;

import Model.Shape;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.util.LinkedList;
import javax.swing.JPanel;

/**
 *
 * @author PhucAnh
 */
public class MyPanel extends JPanel{
    
    public Graphics bufferGraphics = null;
    public Image bufferImage = null;
    private LinkedList<Shape> myObject;
    
    public MyPanel(LinkedList<Shape> myObject) {
        this.myObject = myObject;
    }
    public MyPanel(LinkedList<Shape> myObject,Image bufferImage) {
        this.myObject = myObject;
        this.bufferImage=bufferImage;
    }
    
    public Image getImage(){
        return bufferImage;
    }
    
    public void setImage(Image img){
       bufferImage=img;
    }
    
    public Graphics getGraphic(){
        
        return bufferGraphics;
    }

    @Override
    public void paintComponent(Graphics g) {
         //super.paintComponent(g);
            bufferGraphics=g.create();
            if (bufferImage == null) {
            bufferImage = createImage(this.getSize().width, this.getSize().height);
            bufferGraphics = bufferImage.getGraphics();
            }
            bufferGraphics.setColor(Color.white);
            bufferGraphics.fillRect(0, 0, this.getSize().width, this.getSize().height);
            g.drawImage(bufferImage, 0, 0, this);          
            for(int i =0;i<myObject.size();i++)
            {
                ((Shape)myObject.get(i)).drawShape(g,(BufferedImage)bufferImage);
                
            }
            repaint();
}
}
